package com.fa.dv.controller;

import com.alibaba.excel.EasyExcel;
import com.fa.common.core.controller.BaseController;
import com.fa.common.core.domain.AjaxResult;
import com.fa.common.core.domain.entity.SysUser;
import com.fa.common.core.page.TableDataInfo;
import com.fa.common.utils.poi.ExcelUtil;
import com.fa.dv.domain.Subject;
import com.fa.dv.domain.dto.SubjectDTO;
import com.fa.dv.listener.SubjectReadListener;
import com.fa.dv.service.ISubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/dv/subject")
public class SubjectController extends BaseController {

    @Autowired
    private ISubjectService subjectService;


    /**
     * 查询列表
     * @param subjectDTO
     * @return
     */
    @GetMapping("/list")
    public TableDataInfo list(SubjectDTO subjectDTO){
        List<Subject> list = subjectService.selectList(subjectDTO);
        return getDataTable(list);
    }


    /**
     * 新增
     * @param subject
     * @return
     */
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Subject subject){
        subjectService.insert(subject);
        return AjaxResult.success();
    }
    /**
     * 修改
     * @param subject
     * @return
     */
    @PostMapping("/update")
    public AjaxResult update(@RequestBody Subject subject){
        subjectService.update(subject);
        return AjaxResult.success();
    }
    /**
     * 根据ID查询
     * @param subjectId
     * @return
     */
    @GetMapping("/getById")
    public AjaxResult getById(Integer[] subjectId){
        int id = subjectId[0];
        Subject subject = subjectService.selectById(id);
        return AjaxResult.success(subject);
    }

    /**
     * 根据ID删除
     * @param subjectId
     * @return
     */
    @GetMapping("/deleteById")
    public AjaxResult deleteById(Integer subjectId){
        System.out.println("subjectId = " + subjectId);
        subjectService.delete(subjectId);
        return AjaxResult.success();
    }

    @GetMapping("/deleteList")
    public AjaxResult deleteList(@RequestParam("subjectIds") List<Integer> subjectIds){
        System.out.println("subjectIds = " + subjectIds);
        subjectService.deleteList(subjectIds);
        return AjaxResult.success();
    }


    /**
     * 导出模板
     * @param response
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws Exception {
        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("点检保养项目模板", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        ArrayList<Subject> list = new ArrayList<>();
        EasyExcel.write(response.getOutputStream(), Subject.class).sheet("模板").doWrite(list);
    }


    @PostMapping("/export")
    public void exportUnit(SubjectDTO dto,HttpServletResponse response) throws Exception {

        // 查询列表 //
        List<Subject> list = subjectService.selectList(dto);

        // 导出.xlsx用这个
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码
        String fileName = URLEncoder.encode("计量单位信息", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");

        EasyExcel.write(response.getOutputStream(), Subject.class).sheet("点检保养项目列表").doWrite(list);
    }

    /**
     * 导入单位数据
     */
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws IOException {
        EasyExcel.read(file.getInputStream(),
                Subject.class,
                new SubjectReadListener(subjectService)).sheet().doRead();
        return AjaxResult.success();
    }
    /**
     * 统计近7天登录次数
     */
    @GetMapping("/logEcharts")
    public AjaxResult getLogEcharts() {
        /**
         * {
         *   code:200,
         *   msg: "",
         *   data:{
         *     xAxisData: ['2025-10-01', '2025-10-02', '2025-10-03', '2025-10-04', '2025-10-05', '2025-10-06', '2025-10-07'],
         *     seriesData: [150, 230, 224, 218, 135, 147, 260]
         *   }
         * }
         */
        // 调用service查询近7天登录次数
        Map<String,ArrayList<? extends Object>> map = subjectService.getLogEcharts7Day();
        return AjaxResult.success(map);
    }
}
